<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Copy</title>
</head>
<body>
  <textarea></textarea>
  <p>123123123213</p>
  <button>Copy</button>
  <script>
    const text = document.querySelector('textarea');
    const p = document.querySelector('p');
    const btn = document.querySelector('button');
    btn.addEventListener('click', (ev) => {
      console.log(text)
      const selection = window.getSelection();
      const range = document.createRange();

      range.selectNodeContents(p);
      selection.removeAllRanges();
      selection.addRange(range);
      // document.execCommand('paste')
      selectedText = selection.toString();
      console.log(selectedText);
      // text.focus();
      document.execCommand('copy')
    })
  </script>
</body>
</html>